package com.ljxt.dt.dao.storemanagementdao.impl;

import com.ljxt.dt.dao.storemanagementdao.StoreDao;
import com.ljxt.dt.domain.*;
import com.ljxt.dt.util.DaoUtil;
import com.ljxt.dt.util.ServiceUtil;
import com.ljxt.dt.util.ServletUtil;
import com.ljxt.dt.vo.storemanagement.FindVO;
import com.ljxt.dt.vo.storemanagement.ReceptDetailVO;
import com.ljxt.dt.vo.storemanagement.ReceptVO;
import com.sun.org.apache.regexp.internal.RE;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-11 19:13
 * @copyright 老九学堂
 */

public class StoreDaoImpl  implements StoreDao {
    @Override
    public List<ReceptInfo> findAll(int pageIndex, int pageSize, FindVO findVO) {
        String sql = "select * from recept_info";

        Map<String,List<Object>> result = DaoUtil.getSql(findVO,sql);
        List<Object> params = new ArrayList<>();
        for (String s: result.keySet()) {
            sql = s;
            params = result.get(s);
        }


        //判断是否有开始时间和结束时间
        if(findVO.getStartTime() != null){
           sql += " and createdate >= ? ";
           params.add(findVO.getStartTime());
        }
        if(findVO.getEndTime() != null){
            sql += "and createdate <= ?";
            params.add(findVO.getEndTime());
        }
        sql += " ORDER BY  state_id,createdate desc";
        sql+=" limit "+ (pageIndex-1)*pageSize +" ,"+pageSize;

        return DaoUtil.querySelectAll(sql,new ReceptInfo(),params.toArray());
    }
    @Override
    public int selectTotle(FindVO findVO) {
        String sql = "select count(*) from recept_info";

        Map<String,List<Object>> result = DaoUtil.getSql(findVO,sql);
        List<Object> params = new ArrayList<>();
        for (String s: result.keySet()) {
            sql = s;
            params = result.get(s);
        }


        //判断是否有开始时间和结束时间
        if(findVO.getStartTime() != null){
            sql += " and createdate >= ? ";
            params.add(findVO.getStartTime());
        }
        if(findVO.getEndTime() != null){
            sql += "and createdate <= ?";
            params.add(findVO.getEndTime());
        }


        return DaoUtil.querySelectCount(sql,params.toArray());
    }

    @Override
    public String selectInfoValue(int id) {
        String sql = "select handler_type_name from handler_type where id = ?";
        return DaoUtil.querySelectString(sql,id);
    }

    @Override
    public String selectInfoResult(int id) {
        String sql = "select result from handler_result where id = ?";
        return DaoUtil.querySelectString(sql,id);
    }

    @Override
    public int submitOrder(String order) {
        String sql = "update recept_info set state_id = 2 where recept_no = ?";
        return DaoUtil.updateDate(sql,order);
    }

    @Override
    public int insertBill(String order, String name,Integer typeId,Integer resultId) {
        String date =  ServiceUtil.dateFormat(new Date());
        String sql = "INSERT INTO `dt`.`handler_info`(`order_no`, `name`, `handler_type_id`, `result_id`, `time`) VALUES (?, ?, ?, ?,?)";
        return DaoUtil.updateDate(sql,order,name,typeId,resultId,date);
    }

    @Override
    public int checkOrder(String datum) {
        String sql = "update recept_info set state_id = 4 where recept_no = ?";
        return DaoUtil.updateDate(sql,datum);
    }

    @Override
    public int denyCheckOrder(String datum) {
        String sql = "update recept_info set state_id = 3 where recept_no = ?";
        return DaoUtil.updateDate(sql,datum);
    }

    @Override
    public int delete(String order) {
        String sql = "DELETE FROM `dt`.`recept_info` WHERE `recept_no` = ?";
        return DaoUtil.updateDate(sql,order);
    }

    @Override
    public int deleteInfo(String order) {
        String sql = "DELETE FROM `dt`.`recept_detail` WHERE `recept_no` = ?";

        return DaoUtil.updateDate(sql,order);
    }

    @Override
    public int submitInfo(String receptNo) {
        String sql = "update recept_info set state_id = 5 where recept_no = ?";
        return DaoUtil.updateDate(sql,receptNo);
    }

    @Override
    public int insertStock(ReceptDetailVO receptDetailVO) {
        String sql = "INSERT INTO `dt`.`stock_detail`(`goods_id`, `attr_id`, `warehouse_id`, `shelves_id`, `total`, `complete_amount`, `broken_amount`) VALUES (?, ?, ?, ?, ?, ?, ?)";
        List<Object> list = new ArrayList<>();
        //先查询数据库是否已经有该商品了
        String selectSql = "select * from stock_detail where attr_id = ?";
        StockDetail   result = DaoUtil.querySelectOnly(selectSql,new StockDetail(),receptDetailVO.getAttrId());
        //如果没找到则增加
        if(result == null){
            list.add(receptDetailVO.getGoodsId());
            list.add(receptDetailVO.getAttrId());
            list.add(receptDetailVO.getWarehouseId());
            list.add(receptDetailVO.getShelvesId());
            list.add(receptDetailVO.getCompleteAmount());
            list.add(receptDetailVO.getCompleteAmount()-receptDetailVO.getBrokenAmount());
            list.add(receptDetailVO.getBrokenAmount());
            return  DaoUtil.updateDate(sql,list.toArray());
        }else {
                //更新总数
            String updateSql = "UPDATE `dt`.`stock_detail` SET `total` = ?, `complete_amount` = ?, `broken_amount` = ? WHERE `id` = ?";
            List<Object> params = new ArrayList<>();
            params.add(result.getTotal()+(receptDetailVO.getCompleteAmount()-receptDetailVO.getBrokenAmount()));
            params.add(result.getCompleteAmount()+(receptDetailVO.getCompleteAmount()-receptDetailVO.getBrokenAmount()));
            params.add(result.getBrokenAmount()+receptDetailVO.getBrokenAmount());
            params.add(result.getId());
            //更新库存的最后更新时间
            String lastSql  = "UPDATE `dt`.`stock` SET `lastdate` = ? WHERE goods_id = ?";
            Date date = new Date();
            DaoUtil.updateDate(lastSql,date,result.getGoodsId());

            return  DaoUtil.updateDate(updateSql,params.toArray());
        }

    }

    @Override
    public int updateBill(ReceptDetailVO receptDetailVO) {


        //更新订单对应的商品信息
        String sql = "UPDATE `dt`.`recept_detail` SET `shelves_id` = ? WHERE `recept_no` = ? and goods_id = ?";
        List<Object> list = new ArrayList<>();
        list.add(receptDetailVO.getShelvesId());
        list.add(receptDetailVO.getReceptNo());
        list.add(receptDetailVO.getGoodsId());
        return DaoUtil.updateDate(sql,list.toArray());
    }

    @Override
    public int insertStockGoodsInfo(ReceptDetailVO receptDetailVO) {
        String sql  = "INSERT INTO `dt`.`stock`(`lastdate`, `goods_id`, `goods_name`, `unit`, `out_amount`, `put_amount`) VALUES (?, ?,?,?,?, ?)";
        //先判断该库存是否已经有该商品
        Stock stock = DaoUtil.querySelectOnly("select * from stock where goods_id = ?",new Stock(), receptDetailVO.getGoodsId());
        if(stock == null){
            //增加
            Date date = new Date();
            List<Object> params = new ArrayList<>();
            params.add(date);
            params.add(receptDetailVO.getGoodsId());
            params.add(receptDetailVO.getGoodsName());
            //获得计量单位
            String unit = DaoUtil.querySelectString("select unit from goods_info where id = ?",receptDetailVO.getGoodsId());
            params.add(unit);
            params.add(0);
            params.add(0);
            return  DaoUtil.updateDate(sql,params.toArray());
        }else {
            //更新最后更新时间
            String updateSql = "UPDATE `dt`.`stock` SET `lastdate` = ? WHERE `goods_id` = ?";

            return DaoUtil.updateDate(updateSql,new Date(),receptDetailVO.getGoodsId());
        }


    }


    @Override
    public List<String> selectName() {
        return null;
    }

    @Override
    public List<ReceptType> selectType() {
        String sql="select * from recept_type ";
        return DaoUtil.querySelectAll(sql,new ReceptType());
    }

    @Override
    public List<BillState> selectResult() {
        String sql = "select * from  bill_state";
        return DaoUtil.querySelectAll(sql,new BillState());
    }

    @Override
    public String selectResultString(int id) {
        String sql = "select bill_state_name from bill_state where id =  ?";
        return DaoUtil.querySelectString(sql,id);
    }

    @Override
    public String selectName(int id) {
        String sql = "select name from recept_type where id =  ?";
        return DaoUtil.querySelectString(sql,id);
    }


}
